﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DVDFilmoteka.Sorting;
using System.Windows.Forms;
using MovieStorage;

namespace DVDFilmoteka.Sorting
{
    class CSFDRatingSorter : ISorter
    {
        private readonly List<String> expanded = new List<string>();

        #region ISorter Members

        public void Sort(TreeView treeView, List<MovieDataStore12> movieList)
        {
            List<String> hodnoceniNodeList = getNodeList();
            hodnoceniNodeList.Reverse();

            // Smazat
            treeView.Nodes.Clear();

            // Pridat uzli s pocatcnimy pismeny
            foreach (var item in hodnoceniNodeList)
            {
                treeView.Nodes.Add(item);
            }

            // Naplnit uzli nazvy filmu
            foreach (var item in movieList)
            {
                int index = 0;
                String name = Properties.Messages.messages_nomoviename;
                if (!string.IsNullOrEmpty(item.czechName))
                {
                    name = item.czechName;
                }
                else if (!string.IsNullOrEmpty(item.originalName))
                {
                    name = item.originalName;
                }

                if (String.IsNullOrEmpty(item.csfdRating))
                {
                    index = 0;
                }
                else
                {
                    String rating = item.csfdRating;
                    if (item.csfdRating.Contains("%")) rating = rating.Substring(0, rating.IndexOf("%"));

                    int irating = 0;
                    if (Int32.TryParse(rating, out irating))
                    {
                        if (irating <= 19) index = 1;
                        if (irating > 19 && irating <= 39) index = 2;
                        if (irating > 39 && irating <= 59) index = 3;
                        if (irating > 59 && irating <= 79) index = 4;
                        if (irating > 79 && irating <= 100) index = 5;
                    }
                    else
                    {
                        index = 0;
                    }
                }
                index = treeView.Nodes.Count - index - 1;

                if (index < 0 || index >= treeView.Nodes.Count) index = 0;

                TreeNode node = new TreeNode(name);
                node.Tag = item; // V treelistu si drzim ukazatel na data o filmu

                treeView.Nodes[index].Nodes.Add(node);
            }

            // Vymazat uzli, ktere jsou prazdne
            for (int i = 0; i < treeView.Nodes.Count; i++)
            {
                TreeNode item = treeView.Nodes[i];
                if (item.Nodes == null || item.Nodes.Count == 0)
                {
                    treeView.Nodes.Remove(item);
                    i--;
                }
            }

            // Expanduje korenove nody pokud byli predtim expandovane
            if (expanded != null && expanded.Count > 0)
            {
                foreach (TreeNode item in treeView.Nodes)
                {
                    if (expanded.Contains(item.Text))
                    {
                        item.Expand();
                    }
                }
            }
        }

        public void SaveExpanded(TreeView treeView)
        {
            expanded.Clear();
            foreach (TreeNode item in treeView.Nodes)
            {
                if (item.IsExpanded)
                {
                    expanded.Add(item.Text);
                }
            }
        }

        public ESorters Category()
        {
            return ESorters.CSFDRATING;
        }

        public string Name()
        {
            return Properties.Messages.sorter_CSFDRATING;
        }

        #endregion

        private List<String> getNodeList()
        {
            List<String> list = new List<string>();

            list.Add("N/A");
            list.Add("0-19%");
            list.Add("20-39%");
            list.Add("40-59%");
            list.Add("60-79%");
            list.Add("80-100%");            
            return list;
        }
    }
}
